import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import qs from 'qs'

axios.defaults.timeout = 2000000
//axios.defaults.withCredentials = true
//axios.defaults.headers.common['token'] = window.localStorage.getItem('token');
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' // 配置请求头
//axios.defaults.headers.post['Content-Type'] = 'application/vnd.ms-excel'
// 添加一个请求拦截器

axios.interceptors.request.use(function (config) {
  if (window.sessionStorage.getItem('token')) {
    // 让每个请求携带自定义token 请根据实际情况自行修改
    config.headers['token'] = window.sessionStorage.getItem('token')
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {

  let code = response.data.code
  if (response.status === 200) {
    if (code === 0) {
      return response.data
    } else if (code === -1) { // 错误提示
      MessageBox.alert(response.data.datas, '网络异常，请重试！', response.data.info, {})
    } else {  //没有状态码，默认是文件流
      return Promise.resolve(response);
    }

  } else {
    return Promise.reject(response);
  }


}, function (error) {

  MessageBox.alert(response.data.datas, '网络异常，请重试！', response.data.info, {})
  return Promise.reject(error)
  // Do something with response error
  // return Promise.reject(error)
})


export const base = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : window.location.protocol + '//' + window.location.host
// 接口代理地址参见：config/index.js中的proxyTable配置

// 通用方法
export const POST = (url, params) => {
  return axios.post(`${base}${url}`, params).then(res => res.data)
}
//  文件流
export const POSTDown = (url, params) => {
  return axios.post(`${base}${url}`, params, { responseType: 'blob' }).then(res => res)
}


export const GET = (url, params) => {
  return axios.get(`${base}${url}`, { params: params }).then(res => res.data)
}

export const PUT = (url, params) => {
  return axios.put(`${base}${url}`, params).then(res => res.data)
}

export const DELETE = (url, params) => {
  return axios.delete(`${base}${url}`, { params: params }).then(res => res.data)
}

export const PATCH = (url, params) => {
  return axios.patch(`${base}${url}`, params).then(res => res.data)
}
// post请求
export const postReq = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    data: params
  });
};

// post请求
export const postReq1 = (url, params) => {
  return axios({
    method: 'get',
    url: `${url}`,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    param: params
  });
};
// get请求
export const getReq = (url, params) => {
  return axios({
    method: 'get',
    url: `${base}${url}`,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    params: params
  });
};

// post form请求
export const postReqForm = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(params)
  });
};

// get form请求
export const getReqForm = (url, params) => {
  return axios({
    method: 'get',
    url: `${base}${url}`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    params: params
  });
};
// get form请求
export const newgetReqForm = (url, params) => {
  return axios({
    method: 'get',
    url: `${base}${url}`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    params: params
  });
};
// get form请求
export const newpostReqForm = (url, params) => {
  return axios({
    method: 'post',
    url: `${url}`,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    data: params
  });
};